/**
 * 匠言知识付费v2.0.0
 * Author: 山西匠言网络科技有限公司
 * 这不是一个免费软件，它受限于许可条款，你可以访问https://www.zsfzxkc.cn/获取更多详细信息。
 * This is not a free software, it under the license terms, you can visit https://www.zsffzxkc.cn/ get more details.
 */
<template>
	<view class="container">
		
	<!-- 保存 -->
	<view class="baocun">
		<text @click="addnew">新增收货地址</text>
	</view>
		
		
		<!-- 地址列表 -->
		<radio-group @change="radioChange">
		<view class="address-info" v-for="(item, index) in addressInfo" :key="index" @click="check_ok(item.id)">
			<view class="address-info-top">
				<view class="shrdh">
					<text>收货人：{{item.shr_name}}</text>
					<text>{{item.phone}}</text>
				</view>
				<text>{{item.area + item.address}}</text>
			</view>
			<view class="address-info-bottom">
				<label>
					<radio :value="item.id" :checked="item.is_mr === 1" />
					<text>默认地址</text>
				</label>
				<view class="address-info-bottom-right">
					<view class="bianji" @click="editAddress(item.id)">
						<image src="../../../static/bianji.png" mode=""></image>
						<text>编辑</text>
					</view>
					<view class="shanchu" @click="deleteAddress(item.id)">
						<image src="../../../static/shanchu.png" mode=""></image>
						<text>删除</text>
					</view>
				</view>
			</view>
		</view>
		</radio-group>
		
		
	</view>
</template>

<script>
	import {checkUserinfo} from '@/request/checkUserinfo';
	export default {
		data() {
			return {
				isDefault: 0,
				addressInfo: [],
				orderpage:false
			};
		},
		onShareAppMessage(res) {
			let path = getCurrentPages()
			let path_share = path[0].$page.fullPath
			let path_title = path[0].data.title
			let userinfo = uni.getStorageSync('userinfo')
			let base_set = uni.getStorageSync('base_set')
			if(userinfo.uid=='' || !userinfo.uid){
				uni.navigateTo({
					url:'../login/login'
				})
				return {
					title: '请先登录后再分享给好友',
					path: ''
				}
			}else{
				if (res.from === 'button') {
					
				}
				return {
					title: base_set.title,
					path: `${path_share}?pid=${userinfo.uid}`
				}
			}
		},
		onLoad(e) {
			checkUserinfo();
			if(e.orderpage==1){
				this.orderpage=true
			}
			this.getaddresslist()
		},
		methods: {
			check_ok(id){
				if(this.orderpage){
					uni.setStorageSync('checked_addressid',id)
					uni.navigateBack()
				}else{
					return false
				}
			},
			getaddresslist(){
				const userinfo_check = uni.getStorageSync('userinfo');
				let uid=userinfo_check.uid
				const BASE_URL=uni.BASE_URL
				uni.request({
					url: BASE_URL+'index/user/address_list',
					data: {uid:uid},
					method:'POST',
					success:(res) =>{
						console.log(res.data);
						this.addressInfo =res.data.data
					},
					fail:(res)=> {
						console.log(res.data);
					}
				});
			},
			addnew(){
				uni.navigateTo({
					url: '/pages/mall/address/address'
				});
			},
			radioChange (e) {
				console.log(e.detail)
				const userinfo_check = uni.getStorageSync('userinfo');
				let uid=userinfo_check.uid
				const BASE_URL=uni.BASE_URL
				uni.request({
					url: BASE_URL+'index/user/set_mr_address',
					data: {id:e.detail.value,uid:uid},
					method:'POST',
					success:(res) =>{
						console.log(res.data);
					},
					fail:(res)=> {
						console.log(res.data);
					}
				});
			},
			// 编辑地址
			editAddress(id) {
				uni.navigateTo({
					url: '/pages/mall/address/address?id='+id
				});
			},
			// 删除地址
			deleteAddress(id) {
				uni.showModal({
					title: '提示',
					content: '确定删除该地址吗？',
					showCancel: true,
					cancelText: '取消',
					confirmText: '确认',
					success: res => {
						console.log(res)
						if(res.cancel) {
							console.log('点击了取消')
						} else if(res.confirm) {
							const BASE_URL=uni.BASE_URL
							uni.request({
								url: BASE_URL+'index/user/del_address',
								data: {id:id},
								method:'POST',
								success:(res) =>{
									console.log(res.data);
									uni.showToast({
										title:'删除成功'
									})
									uni.navigateTo({
										url:'/pages/mall/shipping-address/shipping-address'
									})
								},
								fail:(res)=> {
									console.log(res.data);
								}
							});
						}
					},
				});
			}
		}
	}
</script>

<style lang="less" scoped>
.baocun {
	width: 100%;
	height: 100upx;
	background-color: #fff;
	position: absolute;
	bottom: 0;
	display: flex;
	align-items: center;
	justify-content: center;
	text {
		width: 690upx;
		height: 88upx;
		border-radius: 10upx;
		background-color: #fd5151;
		font-size: 33upx;
		color: #fff;
		text-align: center;
		line-height: 88upx;
	}
}	
// 管理收货地址
.glshdz {
	height: 100upx;
	background-color: #fff;
	text-align: center;
	line-height: 100upx;
	text {
		font-size: 32upx;
		font-weight: 700;
		color: #000;
	}
}


// 收货列表
.address-info {
	padding: 0 30upx;
	background-color: #fff;
	margin-bottom: 14upx;
	&-top {
		height: 180upx;
		padding-top: 46upx;
		box-sizing: border-box;
		border-bottom: 2upx solid #e3e3e3;
		.shrdh {
			font-size: 28upx;
			font-weight: 700;
			color: #3c3c3c;
			display: flex;
			justify-content: space-between;
			text:nth-child(2) {
				margin-top: 10upx;
			}
		}
		text {
			font-size: 28upx;
			font-weight: 700;
			color: #3c3c3c;
		}
	}
	&-bottom {
		height: 75upx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		label {
			text {
				font-size: 23upx;
				font-weight: 700;
				color: #fe1446;
				margin-left: 10upx;
			}
		}
		&-right {
			display: flex;
			.bianji {
				display: flex;
				align-items: center;
				image {
					width: 30upx;
					height: 30upx;
				}
				text {
					font-size: 22upx;
					font-weight: 700;
					color: #3c3c3c;
					margin-left: 20upx;
				}
			}
			.shanchu {
				margin-left: 70upx;
				display: flex;
				align-items: center;
				image {
					width: 30upx;
					height: 30upx;
				}
				text {
					font-size: 22upx;
					font-weight: 700;
					color: #3c3c3c;
					margin-left: 20upx;
				}
			}
		}
	}
}




</style>
